import modelEnhance from '@/utils/modelEnhance';
import PageHelper from '@/utils/pageHelper';
/**
 * 当第一次加载完页面时为true
 * 可以用这个值阻止切换页面时
 * 多次初始化数据
 */
let LOADED = false;
export default modelEnhance({
  namespace: 'exams',

  state: {
    pageData: PageHelper.create(),
    categoryList: [],
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/exams' && !LOADED) {
          // LOADED = true;
          dispatch({
            type: 'init'
          });
        }
      });
    }
  },

  effects: {
    // 进入页面加载
    *init({ payload }, { call, put, select }) {
      const { pageData } = yield select(state => state.exams);
      yield put({
        type: 'getPageInfo',
        payload: {
          pageData: pageData.startPage(1, 10)
        }
      });

      yield put.resolve({
        type: '@request',
        afterResponse: resp=> resp.data.map(item => {return {id: item.id, name: item.categoryName, child: item.categoryList}}),
        payload: {
          valueField: 'categoryList',
          method: 'GET',
          url: '/api/lesson/categoryList'
        }
      });
    },
    // 获取分页数据
    *getPageInfo({ payload }, { call, put }) {
      const { pageData } = payload;
      yield put({
        type: '@request',
        payload: {
          valueField: 'pageData',
          url: '/api/exam/listForManage',
          pageInfo: pageData
        }
      });
    },
    // 保存 之后查询分页
    // *save({ payload }, { call, put, select, take }) {
    //   const { values, success } = payload;
    //   const { pageData } = yield select(state => state.exams);
    //   // put是非阻塞的 put.resolve是阻塞型的
    //   yield put.resolve({
    //     type: '@request',
    //     payload: {
    //       notice: true,
    //       url: '/exams/save',
    //       data: values
    //     }
    //   });
    //
    //   yield put({
    //     type: 'getPageInfo',
    //     payload: { pageData }
    //   });
    //   success();
    // },
    // 修改
    *update({ payload }, { call, put, select}) {
      const url = '/api/exam/examPaper/updateExamPaperBasic';

      const {record, success, values} = payload;
      const { pageData } = yield select(state => state.exams);
      if(!values.passScore) values.passScore = 0;
      if(!values.description) values.description = '';

      yield put.resolve({
        type: '@request',
        payload: {
          url,
          notice: true,
          data: values,
        }
      });
      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
      success();

    },
    // 删除 之后查询分页
    *remove({ payload }, { call, put, select, all }) {
      const { records, success } = payload;
      const { pageData } = yield select(state => state.exams);
      yield all( records.map(item=> put({
        type: '@request',
        payload: {
          notice: true,
          method: 'DELETE',
          url: '/api/exam/examPaper/deletePaper/examId/' + item.id,
          // data: records.map(item => item.id)
        }
      })));
      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
      success();
    },
    *import({payload}, {call, put, select}){
      const { values, success } = payload;
      const { pageData } = yield select(state => state.exams);

      if (values.tmpl && values.tmpl.length> 0) {
        values.tmpl = values.tmpl[0].originFileObj
      }
      if(!values.passScore) values.passScore = 0;
      if(!values.description) values.description = '';

      yield put.resolve({
        type: '@request',
        payload: {
          url: '/api/exam/examPaper/upload',
          notice: true,
          headers: {'content-type': 'multipart/form-data'},
          data: values,
        }
      });
      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
      success();
    },

    *publishExam({payload}, {call, put, select}){
      const {record} = payload;
      const { pageData } = yield select(state => state.exams);

      yield put.resolve({
        type: '@request',
        payload:{
        url: '/api/exam/examPaper/publish',
        data: record.id,
        notice: true,},
      });
      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
    }
  },

  reducers: {}
});
